package com.jerry.alibaba.transaction.controller;

import com.jerry.alibaba.transaction.entity.Account;
import com.jerry.alibaba.transaction.entity.CommonResult;
import com.jerry.alibaba.transaction.entity.Storage;
import com.jerry.alibaba.transaction.service.AccountService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
public class AccountController {

    @Resource
    private AccountService accountService;

    @PostMapping(value="/account/decrease")
    public CommonResult decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money) {
        accountService.decrease(userId, money);
        return new CommonResult(200, "扣减账户成功");
    }

    @PostMapping(value="/account/create")
    public CommonResult create(@RequestBody Account account) {
        int result = accountService.create(account);
        if (result > 0) {
            return new CommonResult(200, "账户创建成功");
        } else {
            return new CommonResult(444, "账户创建失败");
        }

    }

    @GetMapping(value="/account/get/{id}")
    public CommonResult getById(@PathVariable("id") Long id) {
        Account account = accountService.getById(id);
        if (account != null) {
            return new CommonResult(200, "查询成功", account);
        } else {
            return new CommonResult(444, "查询失败，查询id="+id);
        }
    }

    @GetMapping(value="/account/get")
    public CommonResult getList() {
        List<Account> accountList = accountService.getList();
        if (accountList != null) {
            return new CommonResult(200, "查询成功", accountList);
        } else {
            return new CommonResult(444, "查询失败");
        }
    }

}
